package com.ylr.blog.framework.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylr.user.common.enums.UserMessageEnum;
import com.ylr.user.framework.resolver.YuUserMessageResolver;
import com.ylr.base.common.constant.Constant;
import com.ylr.base.common.dto.PageDto;
import com.ylr.base.common.enums.CommonEnum;
import com.ylr.base.common.enums.MqEnum;
import com.ylr.base.common.enums.ResultEnum;
import com.ylr.base.common.exception.YlrException;
import com.ylr.base.common.po.user.UserPo;
import com.ylr.base.common.tool.PageQuery;
import com.ylr.base.common.utils.UserUtils;
import com.ylr.base.framework.service.SnowflakeService;
import com.ylr.basic.framework.resolver.YbAesResolver;
import com.ylr.blog.common.bo.publish_blog.PublishBlogBo;
import com.ylr.blog.common.dto.blog.BlogDto;
import com.ylr.blog.common.enums.BlogCodeEnum;
import com.ylr.blog.common.enums.BlogEnum;
import com.ylr.blog.common.enums.BlogResultEnum;
import com.ylr.blog.common.vo.blog.BlogFormVo;
import com.ylr.blog.common.vo.blog.BlogVo;
import com.ylr.blog.framework.entity.AuditBlogEntity;
import com.ylr.blog.framework.entity.BlogEntity;
import com.ylr.blog.framework.manager.AuditBlogManager;
import com.ylr.blog.framework.manager.BlogContentManager;
import com.ylr.blog.framework.manager.BlogFlowManager;
import com.ylr.blog.framework.manager.BlogTagRelationManager;
import com.ylr.blog.framework.mapper.BlogMapper;
import com.ylr.blog.framework.service.*;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-18 21:39:06
 * className: BlogServiceImpl
 * version: 1.0
 * description:
 */
@Service("blogService")
@AllArgsConstructor
public class BlogServiceImpl extends ServiceImpl<BlogMapper, BlogEntity> implements BlogService {

    private static final Logger log = LoggerFactory.getLogger(BlogServiceImpl.class);

    private final BlogCodeService blogCodeService;

    private final BlogFlowService blogFlowService;

    private final AuditBlogService auditBlogService;

    private final PublishBlogService publishBlogService;

    private final BlogFlowManager blogFlowManager;

    private final BlogContentManager blogContentManager;

    private final BlogTagRelationManager blogTagRelationManager;

    private final AuditBlogManager auditBlogManager;

    private final SnowflakeService snowflakeService;

    private final YbAesResolver ybAesResolver;

    private final RabbitTemplate rabbitTemplate;

    private final YuUserMessageResolver yuUserMessageResolver;

    @Transactional
    @Override
    public void  saveBlog(BlogVo blogVo) {
        // 获取正在保存博客的用户
        UserPo userPo = UserUtils.getUser();
        Date date = new Date();

        // 1、博客
        BlogEntity blog;
        if (Objects.isNull(blogVo.getId())) {
            // 新博客
            blog = this.getNewBlog(blogVo, userPo, date);

            // 保存博客
            baseMapper.insert(blog);
            log.info("保存博客完成");

            // 博客第一次保存，增加博客保存记录流水
            blogFlowManager.saveBlogFlow(blog.getId(), blog.getCode(), BlogEnum.SAVE.getValue(), "新建博客");
        } else {
            // 修改博客
            blog = this.getBlog(blogVo, userPo, date);

            int count = baseMapper.updateById(blog);
            log.info("更新博客完成。count={}", count);
        }

        // 2、保存博客正文
        blogContentManager.saveBlogContent(blog.getId(), blog.getCode(), blogVo.getContent());

        // 3、博客关联标签
        blogTagRelationManager.saveBlogTagRelation(blog.getId(), blog.getCode(), blogVo.getTagIdList());
    }

    @Transactional
    @Override
    public String saveAndCommitBlog(BlogVo blogVo) {

        // 获取正在保存博客的用户
        UserPo userPo = UserUtils.getUser();
        Date date = new Date();

        // 1、博客
        BlogEntity blog;
        if (Objects.isNull(blogVo.getId())) {
            // 新博客
            blog = this.getNewBlog(blogVo, userPo, date);
            blog.setStatus(BlogEnum.AUDIT.getValue());

            if (StringUtils.isNotEmpty(blog.getUnfinishedField())) {
                log.info("博客数据未填写完成，不能保存并提交审核");
                return blog.getUnfinishedField();
            }

            // 保存博客
            baseMapper.insert(blog);
            log.info("保存博客完成");

            // 2、保存博客正文
            blogContentManager.saveBlogContent(blog.getId(), blog.getCode(), blogVo.getContent());
        } else {
            // 修改博客
            blog = this.getBlog(blogVo, userPo, date);
            blog.setStatus(BlogEnum.AUDIT.getValue());

            if (StringUtils.isNotEmpty(blog.getUnfinishedField())) {
                log.info("博客数据未填写完成，不能修改并提交审核");
                return blog.getUnfinishedField();
            }

            int count = baseMapper.updateById(blog);
            log.info("更新博客完成。count={}", count);

            // 2、修改博客正文
            blogContentManager.updateBlogContent(blog.getId(), blog.getCode(), blogVo.getContent());
        }
        // 3、增加博客提交审核记录流水
        blogFlowManager.saveBlogFlow(blog.getId(), blog.getCode(), BlogEnum.AUDIT.getValue(), "博客提交审核");

        // 4、博客关联标签
        blogTagRelationManager.saveBlogTagRelation(blog.getId(), blog.getCode(), blogVo.getTagIdList());

        // 5、添加待审核博客
        auditBlogManager.saveAuditBlog(blogVo);

        return StringUtils.EMPTY;
    }

    @Override
    public PageDto<BlogDto> pageBlogList(BlogFormVo blogFormVo) {

        // 获取请求用户
        Long optUserId = UserUtils.getUserId();

        Page<BlogEntity> page = PageQuery.initPage(blogFormVo);

        LambdaQueryWrapper<BlogEntity> blogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 查询字段
        blogLambdaQueryWrapper.select(BlogEntity::getId, BlogEntity::getUserId, BlogEntity::getCode, BlogEntity::getSpaceId,
                BlogEntity::getTitle, BlogEntity::getPreface, BlogEntity::getCoverPicture, BlogEntity::getType, BlogEntity::getViewNum,
                BlogEntity::getLikeNum, BlogEntity::getCollectNum, BlogEntity::getCommentNum, BlogEntity::getPublishTime,
                BlogEntity::getStatus, BlogEntity::getTopStatus, BlogEntity::getHotStatus, BlogEntity::getCopyrightStatus,
                BlogEntity::getCommentStatus, BlogEntity::getCreateTime);
        // 匹配条件
        blogLambdaQueryWrapper.eq(BlogEntity::getUserId, optUserId)
                .likeRight(StringUtils.isNotBlank(blogFormVo.getCode()), BlogEntity::getCode, blogFormVo.getCode())
                .eq(Objects.nonNull(blogFormVo.getSpaceId()), BlogEntity::getSpaceId, blogFormVo.getSpaceId())
                .like(StringUtils.isNotBlank(blogFormVo.getWord()), BlogEntity::getTitle, blogFormVo.getWord())
                .eq(Objects.nonNull(blogFormVo.getType()), BlogEntity::getType, blogFormVo.getType())
                .eq(Objects.nonNull(blogFormVo.getStatus()), BlogEntity::getStatus, blogFormVo.getStatus())
                .eq(BlogEntity::getDeleteStatus, CommonEnum.DELETE_NO.getValue());
        // 查询数据
        baseMapper.selectPage(page, blogLambdaQueryWrapper);

        List<BlogEntity> blogList = page.getRecords();
        long totalNum = page.getTotal();
        if (blogList.isEmpty()) {
            log.info("用户博客分页数据为空");
            return new PageDto<>();
        }
        // 数据封装
        List<BlogDto> blogDtoList = new ArrayList<>(blogList.size());
        for (BlogEntity blog : blogList) {
            blogDtoList.add(this.getBlogDto(blog));
        }

        return new PageDto<>(blogDtoList, totalNum, blogFormVo.getPageNo(), blogFormVo.getPageSize());
    }

    @Override
    public Integer getBlogNum(Long userId, Long spaceId) {
        return baseMapper.selectBlogNum(userId, spaceId, CommonEnum.DELETE_NO.getValue());
    }

    @Transactional
    @Override
    public String commitBlog(Long id, String code) {

        // 获取正在提交博客审核的用户
        UserPo userPo = UserUtils.getUser();
        Date date = new Date();

        // 1、获取博客
        BlogEntity blog = this.findBlog(id, code);
        if (!userPo.getId().equals(blog.getUserId())) {
            log.warn("博客不属于操作用户。userPo={}", userPo);
            throw new YlrException(ResultEnum.ILLEGAL_FAIL);
        }

        if (StringUtils.isNotEmpty(blog.getUnfinishedField())) {
            log.info("博客数据未填写完成，不能提交审核。{}", blog);
            return blog.getUnfinishedField();
        }
        log.info("博客状态。status={}", BlogEnum.getStatusEnum(blog.getStatus()));

        // 2、判断博客状态是否满足提交审核条件
        BlogEnum[] statusEnums = { BlogEnum.SAVE, BlogEnum.AUDIT_REJECT, BlogEnum.RECHECK_REJECT, BlogEnum.AUDIT_WITHDRAW, BlogEnum.EDIT_BLOG };
        boolean bool = true;
        for (BlogEnum statusEnum : statusEnums) {
            if (statusEnum.getValue().equals(blog.getStatus())) {
                bool = false;
                break;
            }
        }
        if (bool) {
            throw new YlrException(BlogResultEnum.BLOG_STATUS_FAIL);
        }

        // 3、修改博客状态
        BlogEntity _blog = new BlogEntity();
        _blog.setId(id)
            .setCode(code)
            .setStatus(BlogEnum.AUDIT.getValue())
            .setUpdateTime(date)
            .setUpdateUser(userPo.getUsername())
            .setUpdateUserId(userPo.getId());
        // 更新数据
        baseMapper.updateById(_blog);
        log.info("博客状态修改完成");

        // 4、增加博客提交审核记录流水
        blogFlowManager.saveBlogFlow(blog.getId(), code, BlogEnum.AUDIT.getValue(), "博客提交审核");

        // 5、添加待审核博客
        auditBlogManager.addAuditBlog(blog);

        return StringUtils.EMPTY;
    }

    @Transactional
    @Override
    public void withdrawAuditBlog(Long id, String code) {
        // 获取正在撤销待审核博客的用户
        UserPo userPo = UserUtils.getUser();

        // 1、获取博客
        BlogEntity blog = this.findBlog(id, code);
        if (!userPo.getId().equals(blog.getUserId())) {
            log.warn("博客不属于操作用户。userPo={}", userPo);
            throw new YlrException(ResultEnum.ILLEGAL_FAIL);
        }
        log.info("博客状态。status={}", BlogEnum.getStatusEnum(blog.getStatus()));
        if (!BlogEnum.AUDIT.getValue().equals(blog.getStatus())) {
            throw new YlrException(ResultEnum.REFRESH);
        }

        Integer status = BlogEnum.SAVE.getValue();
        Long auditBlogId = CommonEnum.DEFAULT_ID.getLongValue();
        // 2、获取博客提交审核前的状态
        AuditBlogEntity auditBlog = auditBlogService.getLastOne(id);
        if (Objects.nonNull(auditBlog)) {
            status = Optional.ofNullable(auditBlog.getStatus())
                    .filter(_status -> !Constant.NEGATIVE_ONE.equals(_status))
                    .orElse(BlogEnum.SAVE.getValue());
            auditBlogId = auditBlog.getId();
        }

        // 3、修改博客状态
        BlogEntity _blog = new BlogEntity();
        _blog.setId(id)
            .setCode(code)
            .setStatus(status)
            .setUpdateTime(new Date())
            .setUpdateUser(userPo.getUsername())
            .setUpdateUserId(userPo.getId());
        // 更新数据
        baseMapper.updateById(_blog);
        log.info("博客状态修改完成");

        // 4、增加博客撤销审核记录流水
        blogFlowManager.saveBlogFlow(id, code, BlogEnum.AUDIT_WITHDRAW.getValue(), "博客撤销审核");

        // 5、终止博客审核
        auditBlogManager.stopAuditBlog(code, auditBlogId);

    }

    @Override
    public void publishBlog(Long id, String code) {

        // 1、获取博客
        BlogEntity blog = findBlog(id, code);

        // 2、发布博客
        this.publishBlog(blog);
    }

    @Transactional
    @Override
    public void updateBlogStatus(Long id, String code, Integer status) {
        // 获取正在更新博客状态的用户
        UserPo userPo = UserUtils.getUser();
        Date date = new Date();

        // 1、更新博客状态
        LambdaUpdateWrapper<BlogEntity> blogLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        // 设置字段值
        blogLambdaUpdateWrapper.set(BlogEntity::getStatus, status)
                .set(BlogEntity::getUpdateTime, date)
                .set(BlogEntity::getUpdateUser, userPo.getUsername())
                .set(BlogEntity::getUpdateUserId, userPo.getId());
        // 匹配条件
        blogLambdaUpdateWrapper.eq(BlogEntity::getId, id)
                .eq(BlogEntity::getCode, code)
                .eq(BlogEntity::getDeleteStatus, CommonEnum.DELETE_NO);
        // 更新数据
        int count = baseMapper.update(null, blogLambdaUpdateWrapper);
        log.info("修改博客状态完成。count={}", count);
        if (count == 0) {
            log.info("博客不存在");
            throw new YlrException(ResultEnum.REFRESH);
        }

        // 2、增加博客流水
        blogFlowService.saveBlogFlow(id, status, StringUtils.EMPTY);

        // 3、如果审核通过，判断是否自动发布博客
        BlogEnum[] statusEnums = {
                BlogEnum.AUDIT_PASS,
                BlogEnum.RECHECK_PASS
        };
        for (BlogEnum statusEnum : statusEnums) {
            if (statusEnum.getValue().equals(status)) {
                // 获取博客对象
                BlogEntity blog = findBlog(id, code);

                if (BlogEnum.AUTO_PUBLISH_OPEN.getValue().equals(blog.getAutoPublishStatus())) {
                    // 博客设置自动发布，下一步进行博客发布
                    this.publishBlog(blog);
                }
            }
        }
    }

    /**
     * 获取博客实体对象
     * @param id 博客主键
     * @return 博客实体对象
     */
    private BlogEntity findBlog(Long id) {
        BlogEntity blog = baseMapper.selectById(id);
        if (Objects.isNull(blog)) {
            log.warn("博客不存在");
            throw new YlrException(ResultEnum.NOT_FOUND);
        }
        if (CommonEnum.DELETE_YES.getValue().equals(blog.getDeleteStatus())) {
            log.warn("博客已删除");
            throw new YlrException(ResultEnum.NOT_FOUND);
        }
        return blog;
    }

    /**
     * 获取博客实体对象
     * @param id   博客主键
     * @param code 博客编号
     * @return 博客实体对象
     */
    private BlogEntity findBlog(Long id, String code) {
        LambdaQueryWrapper<BlogEntity> blogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 匹配条件
        blogLambdaQueryWrapper.eq(BlogEntity::getId,  id)
                .eq(BlogEntity::getCode, code)
                .eq(BlogEntity::getDeleteStatus, CommonEnum.DELETE_NO.getValue());
        // 查询数据
        List<BlogEntity> blogList = baseMapper.selectList(blogLambdaQueryWrapper);
        if (blogList.isEmpty()) {
            log.warn("博客不存在");
            throw new YlrException(ResultEnum.NOT_FOUND);
        }
        if (blogList.size() > 1) {
            log.warn("博客编号匹配到多条博客数据。code={} {}", code, blogList);
        }
        return blogList.get(0);
    }

    /**
     * 博客参数转化成’新‘博客实体对象
     * @param blogVo 参数对象
     * @param userPo 操作用户
     * @param date   操作日期
     * @return 博客实体对象
     */
    private BlogEntity getNewBlog(BlogVo blogVo, UserPo userPo, Date date) {
        BlogEntity blog = new BlogEntity();
        blog.setId(snowflakeService.getId())
            .setUserId(userPo.getId())
            .setSpaceId(Optional.ofNullable(blogVo.getSpaceId()).orElse(Constant.NEGATIVE_ONE.longValue()))
            .setTitle(Optional.ofNullable(blogVo.getTitle()).orElse(StringUtils.EMPTY))
            .setPreface(Optional.ofNullable(blogVo.getPreface()).orElse(StringUtils.EMPTY))
            .setCoverPicture(Optional.ofNullable(blogVo.getCoverPicture()).orElse(StringUtils.EMPTY))
            .setType(Optional.ofNullable(blogVo.getType()).orElse(BlogEnum.AUTHOR_TYPE.getValue()))
            .setViewNum(Constant.ZERO)
            .setLikeNum(Constant.ZERO)
            .setCollectNum(Constant.ZERO)
            .setCommentNum(Constant.ZERO)
            .setPublishTime(Constant.ONE.longValue())
            .setStatus(BlogEnum.SAVE.getValue())
            .setTopStatus(BlogEnum.TOP_NO.getValue())
            .setHotStatus(BlogEnum.HOT_NO.getValue())
            .setCopyrightStatus(Optional.ofNullable(blogVo.getCopyrightStatus()).orElse(BlogEnum.COPYRIGHT_SHOW.getValue()))
            .setCommentStatus(Optional.ofNullable(blogVo.getCommentStatus()).orElse(BlogEnum.COMMENT_OPEN.getValue()))
            .setAutoPublishStatus(Optional.ofNullable(blogVo.getAutoPublishStatus()).orElse(BlogEnum.AUTO_PUBLISH_OPEN.getValue()))
            .setDeleteStatus(CommonEnum.DELETE_NO.getValue())
            .setUpdateTime(date)
            .setUpdateUser(StringUtils.EMPTY)
            .setUpdateUserId(CommonEnum.DEFAULT_ID.getLongValue())
            .setCreateTime(date);
        // 设置博客编号
        blog.setCode(blogCodeService.generateCode(BlogCodeEnum.getName(blog.getType()), userPo.getId(), blog.getId()));
        // 参数校验
        String unfinishedField = this.getUnfinishedField(blogVo);
        blog.setUnfinishedField(unfinishedField);
        return blog;
    }

    /**
     * 博客参数转化成博客实体对象
     * @param blogVo 参数对象
     * @return 博客实体对象
     */
    private BlogEntity getBlog(BlogVo blogVo, UserPo userPo, Date date) {
        BlogEntity blog = new BlogEntity();
        blog.setId(blogVo.getId())
            .setSpaceId(Optional.ofNullable(blogVo.getSpaceId()).orElse(Constant.NEGATIVE_ONE.longValue()))
            .setTitle(Optional.ofNullable(blogVo.getTitle()).orElse(StringUtils.EMPTY))
            .setPreface(Optional.ofNullable(blogVo.getPreface()).orElse(StringUtils.EMPTY))
            .setCoverPicture(Optional.ofNullable(blogVo.getCoverPicture()).orElse(StringUtils.EMPTY))
            .setType(Optional.ofNullable(blogVo.getType()).orElse(BlogEnum.AUTHOR_TYPE.getValue()))
            .setCopyrightStatus(Optional.ofNullable(blogVo.getCopyrightStatus()).orElse(BlogEnum.COPYRIGHT_SHOW.getValue()))
            .setCommentStatus(Optional.ofNullable(blogVo.getCommentStatus()).orElse(BlogEnum.COMMENT_OPEN.getValue()))
            .setAutoPublishStatus(Optional.ofNullable(blogVo.getAutoPublishStatus()).orElse(BlogEnum.AUTO_PUBLISH_OPEN.getValue()))
            .setUpdateTime(date)
            .setUpdateUser(userPo.getUsername())
            .setUpdateUserId(userPo.getId());
        // 设置博客编号
        blog.setCode(blogCodeService.replaceCodeType(BlogCodeEnum.getName(blog.getType()), blogVo.getCode()));
        // 参数校验
        String unfinishedField = this.getUnfinishedField(blogVo);
        blog.setUnfinishedField(unfinishedField);
        return blog;
    }

    /**
     * 获取未完成的属性提示
     * @param blogVo 参数对象
     * @return 提示消息
     */
    private String getUnfinishedField(BlogVo blogVo) {
        StringBuilder sb = new StringBuilder();
        if (Objects.isNull(blogVo.getSpaceId())) {
            sb.append("所属空间未选择,");
        }
        if (StringUtils.EMPTY.equals(blogVo.getTitle())) {
            sb.append("标题未输入,");
        }
        if (StringUtils.EMPTY.equals(blogVo.getPreface())) {
            sb.append("前言描述未输入,");
        }
        if (StringUtils.EMPTY.equals(blogVo.getCoverPicture())) {
            sb.append("没有封面图片,");
        }
        if (Constant.NEGATIVE_ONE.equals(blogVo.getType())) {
            sb.append("博客类型未选择,");
        }
        // TODO 还有一些参数没有进行判断
        if (CollectionUtils.isEmpty(blogVo.getTagIdList())) {
            sb.append("博客标签未选择,");
        } else {
            int length = blogVo.getTagIdList().size();
            for (int i = 0; i < length; i++) {
                sb.append("博客标签，第").append(i + 1).append("值错误,");
            }
        }
        if (StringUtils.EMPTY.equals(blogVo.getContent())) {
            sb.append("博客内容未输入,");
        }
        if (sb.length() > 1) {
            // 删除最后一个英文逗号
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    /**
     * 博客发布
     * @param blog 博客对象
     */
    private void publishBlog(BlogEntity blog) {
        // 获取正在发布博客的用户
        UserPo userPo = UserUtils.getUser();
        Date date = new Date();

        // 2、发布博客
        BlogEntity _blog = new BlogEntity();
        _blog.setId(blog.getId())
            .setCode(blog.getCode())
            .setStatus(BlogEnum.PUBLISH.getValue())
            .setUpdateTime(date)
            .setUpdateUser(userPo.getUsername())
            .setUpdateUserId(userPo.getId());
        int count = baseMapper.updateById(_blog);
        log.info("博客发布完成。count={}", count);

        // 3、保存博客流水
        blogFlowService.saveBlogFlow(blog.getId(), BlogEnum.PUBLISH.getValue(), StringUtils.EMPTY);

        // 3、保存已发布博客
        publishBlogService.savePublishBlog(blog.getId(), blog.getUserId());

        // 4、发送一条消息到mq，保存博客到ElasticSearch
        PublishBlogBo publishBlogBo = new PublishBlogBo();
        publishBlogBo.setId(blog.getId())
                    .setCode(blog.getCode());
        rabbitTemplate.convertAndSend(MqEnum.PUBLISH_BLOG.getExchange(), MqEnum.PUBLISH_BLOG.getRoutingKey(), publishBlogBo);

        // 5、给用户发送消息
        String message = "您的《" + blog.getTitle() + "》博客已发布，请查阅。";
        yuUserMessageResolver.saveUserMessage(blog.getUserId(), CommonEnum.DEFAULT_ID.getLongValue(), UserMessageEnum.SYSTEM_TYPE, message);
    }

    /**
     * 博客实体数据封装
     * @param blog 博客实体对象
     * @return 封装结果
     */
    private BlogDto getBlogDto(BlogEntity blog) {
        BlogDto blogDto = new BlogDto();
        blogDto.setId(blog.getId().toString())
                .setUserId(blog.getUserId().toString())
                .setCode(blog.getCode())
                .setSpaceId(blog.getSpaceId().toString())
                .setTitle(blog.getTitle())
                .setPreface(blog.getPreface())
                .setCoverPicture(blog.getCoverPicture())
                .setType(blog.getType())
                .setViewNum(blog.getViewNum())
                .setCollectNum(blog.getCollectNum())
                .setCommentNum(blog.getCommentNum())
                .setPublishTime(Optional.ofNullable(blog.getPublishTime()).map(Date::new).orElse(null))
                .setStatus(blog.getStatus())
                .setTopStatus(blog.getTopStatus())
                .setHotStatus(blog.getHotStatus())
                .setCopyrightStatus(blog.getCopyrightStatus())
                .setCommentStatus(blog.getCommentStatus())
                .setAutoPublishStatus(blog.getAutoPublishStatus())
                .setCreateTime(blog.getCreateTime());
        return blogDto;
    }
}